
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_boost/flutter_boost.dart';
import 'http/http.dart';
import 'http/http_utils.dart';
import 'modules/abnormal_order/pages/abnormal_order_page.dart';
import 'modules/abnormal_order/pages/abnormal_orderdetail_page.dart';
import 'modules/incremental_board/pages/yybbcoverage/incrementai_broad_main_page.dart';
import 'modules/incremental_board/pages/yybbpenetration/increementai_pentrationpage.dart';


const EventChannel eventChannel = const EventChannel("samples.flutter.io/nativeCallFlutter");
typedef void NativeCallBack(Object event);
Map<String,NativeCallBack> callbakcs = {};

void main(){

  return  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState()=>_MyAppState();
}

class _MyAppState extends State<MyApp> {
  void _onEvent(Object event)
  {
    if(event is Map){
      String name = event['name'];
      if(callbakcs[name] != null){
        callbakcs[name](event);
      }
    }

  }

  void _onError(Object error)
  {
    setState(() {
      // _changingStatus = "${error}";

    });
  }

  @override
  void initState() {
    eventChannel.receiveBroadcastStream().listen(_onEvent,onError:_onError);
    super.initState();
    FlutterBoost.singleton.registerPageBuilders({
      'abnormalOrderPage': (pageName, params, _) {
        _getHttpBaseConfig(params);
        return new AbnormalOrderPage();
      },
     'abnormalOrderDetailPage': (pageName, params, _) {
       _getHttpBaseConfig(params);
       return AbnormalOrderDetailPage(params: params);
     },
      'incrementalBoradPage': (pageName, params, _) {
        _getHttpBaseConfig(params);
        return IncrementalBoradPage(params: params);
      },
      'incrementailPentrationPage': (pageName, params, _) {
        _getHttpBaseConfig(params);
        return IncrementailPentrationPage();
      },

    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter example',
        builder: FlutterBoost.init(postPush: _onRoutePushed),
        home: Container());
  }

  void _onRoutePushed(String pageName, String uniqueId, Map params, Route route,
      Future _) {
  }


  void _getHttpBaseConfig(Map params) {
    final headersJson = json.encode(params);
    Map<String, dynamic> configMap = json.decode(headersJson);
    if(configMap["baseUrlKey"] != null){
      final baseUrl = configMap["baseUrlKey"];
      Map<String, dynamic> headerMap = configMap["headerkey"];
      HttpUtils.init(baseUrl:baseUrl);
      Http().setHeaders(headerMap);
      // 设置代理
      Map<String, dynamic> proxy = configMap["YYBBProxy"];
      Http().proxy_ip = proxy["hostName"];
      Http().proxy_port = proxy["portName"];
    }

  }
}

